package com.tiennt.android.droidcamreader;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;

public class PreferencesActivity extends PreferenceActivity implements
		OnSharedPreferenceChangeListener {

	public static final String KEY_SOURCE_LANGUAGE_PREFERENCE = "sourceLanguageCodeOcrPref";
	public static final String KEY_REALTIME_OCR_AND_TRANSLATION_PREFERENCE = "realTimeOCR";

	private ListPreference listPreferenceSourceLanguage;

	private SharedPreferences sharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		addPreferencesFromResource(R.xml.preferences);

		sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		listPreferenceSourceLanguage = (ListPreference) getPreferenceScreen()
				.findPreference(KEY_SOURCE_LANGUAGE_PREFERENCE);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		getPreferenceScreen().getSharedPreferences()
				.unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		listPreferenceSourceLanguage.setSummary(LanguageCodeHelper
				.getOcrLanguageName(getBaseContext(), sharedPreferences
						.getString(KEY_SOURCE_LANGUAGE_PREFERENCE,
								CameraActivity.DEFAULT_SOURCE_LANGUAGE_CODE)));

		getPreferenceScreen().getSharedPreferences()
				.registerOnSharedPreferenceChangeListener(this);
	}

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
			String key) {
		// TODO Auto-generated method stub
		if (key.equals(KEY_SOURCE_LANGUAGE_PREFERENCE)) {

			// Set the summary text for the source language name
			listPreferenceSourceLanguage
					.setSummary(LanguageCodeHelper.getOcrLanguageName(
							getBaseContext(),
							sharedPreferences
									.getString(
											key,
											CameraActivity.DEFAULT_SOURCE_LANGUAGE_CODE)));
		}
	}

}
